<!-- 草稿箱 -->


<template>
	<view style="padding: 30rpx;">
		<view class="container">
			<view class="column">
				<view class="note" v-for="(item, index) in notes" :key="index" 
					  @click="goEditNote(item.FileName, item.type)" @longpress="deleteDraft(index)">
					  <view class="title">{{ item.title }}</view>
					  <view class="summary">{{ item.summary }}</view>
				</view>
		    </view>
		</view>
		<view class="noteButton">
			<button class="Btn" style="margin-bottom: 5%;" ref="myButton"
					@click = "loadDraft">加载</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				draftName:[
					
				],
				notes: [
					
				],
			}
		},
		
		onLoad() {
			let fs = uni.getFileSystemManager();
			let upath = wx.env.USER_DATA_PATH;
			let draftpath = upath + "/draft";
			let that = this;
			let index = 0;
			
			this.notes.splice(0,1);
			
			fs.readdir({
				dirPath : draftpath,
				success(res){
					that.draftName = res.files;
					console.log(that.draftName);
				}
			})
		},
		
		methods: {
			goEditNote(query, type){
				console.log("jump");
				let upath = wx.env.USER_DATA_PATH;
				let draftpath = upath + "/draft";
				console.log(type);
				if(type === '1'){
					uni.navigateTo({
						url:'/pages/editNote/editNote?FileName=' + draftpath + "/" + query
					})
				}
				else if(type === '2'){
					uni.navigateTo({
						url:'/pages/SWOT/SWOT?FileName=' + draftpath + "/" + query
					})
				}
				else if(type === '3'){
					uni.navigateTo({
						url:'/pages/5W1H/5W1H?FileName=' + draftpath + "/" + query
					})
				}
				else if(type === '4'){
					uni.navigateTo({
						url:'/pages/timeManageFour/timeManageFour?FileName=' + draftpath + "/" + query
					})
				}
			},
			
			loadDraft(){
				console.log(this.draftName);
				this.notes = [];
				let upath = wx.env.USER_DATA_PATH;
				let draftpath = upath + "/draft";
				let fs = uni.getFileSystemManager();
				let that = this;
				
				fs.readdir({
					dirPath : draftpath,
					success(res){
						that.draftName = res.files;
						console.log(that.draftName);
					}
				})
				
				this.draftName.forEach((i ,index)=>{
					fs.readFile({
						filePath : draftpath + "/" + i,
						encoding : 'utf-8',
						position : 0,
						
						success: function(res){
							console.log("/");
							
							let tmp = res.data;
							let arr = tmp.split("#<<<<<!*&*!>>>>>#");
							
							
							let arrSecondPart = that.clearHtml(arr[1]).substring(0,45);
							that.notes.push({
								title: arr[0],
								summary: arrSecondPart,
								FileName: that.draftName[index],
								type: arr[arr.length - 1]
							})
							
							console.log(that.notes[index].title);
							console.log(that.notes[index].summary);
							console.log(that.notes[index].type);
						},
						fail(res){
							console.error(res);
						}
					});
				})
			},
			
			clearHtml(msg) {
			    var msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
			    msg = msg.replace(/[|]*\n/, ''); //去除行尾空格
			    msg = msg.replace(/&npsp;/gi, ''); //去掉npsp
			    return msg;
			},
			
			
			deleteDraft(index){
				let upath = wx.env.USER_DATA_PATH;
				let draftpath = upath + "/draft";
				let that = this;
				let fs = uni.getFileSystemManager();
								
				fs.readdir({
					dirPath : draftpath,
					success(res){
						that.draftName = res.files;
						console.log(that.draftName);
					}
				})
								
								
				const filePath = draftpath + '/' + that.draftName[index]; // 替换为实际文件路径
								
				uni.showModal({
					title: '提示',
					content: '确定删除所选草稿?',
					success: (res) => {
						if (res.confirm) {
							// 删除文件
							fs.unlink({
								filePath: filePath,
								success(res) {
								console.log('文件删除成功');
									uni.showToast({
										icon:'success',
										title:'删除成功',
										duration: 800
									})
									that.loadDraft();
								},
								fail(err) {
									console.error('文件删除失败', err);
								}
							});
						}
					}
				})

			}
		},
		
		mounted(){
			this.$nextTick(() => {
				uni.$emit('clickMyButton');
			});
			
			uni.$on('clickMyButton', () => {
				this.loadDraft();
			});
		}
	}
</script>

<style scoped>
	.container{
		background-color: white;
		padding: 20rpx;
		border-radius: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, .1);
		display: flex;
		justify-content: space-between;
	}
	
	.column{
		margin-left: 5%;
		width: 90%;
	}
	
	.note {
		background-color: #f8f8f8;
		margin-bottom: 10px;
		padding: 10px;
		border-radius: 5px;
		word-wrap: break-word;
	}
	
	.title {
		font-weight: bold;
	}
	
	.summary {
		margin-top: 5px;
		color: #666;
	}
	
	.noteButton{
		display: flex;
		position: fixed;
		bottom: 0;
		width: 100%;
	}
	
	.Btn{
		border-radius: 10px;
		cursor: pointer;
		font-size: 16px;
		margin-right: 5%;
	}
	
</style>
